<template>
    <el-dialog title="选择输入文件"
               :visible.sync="fileVisible"
               :close-on-click-modal="false"
               width="40%"
               v-dialogDrag>
        <div style="overflow: hidden;">
            <el-tabs v-model="activeName" @tab-click="handleClick" style="height: 100%;max-height: 380px">
                <el-tab-pane label="数据空间" name="first" style="height: 100%;overflow: hidden">
                    <el-tree
                            :props="props"
                            :load="loadNode"
                            :data="data"
                            lazy
                            ref="tree"
                            node-key="label"
                            @node-click="handleNode"
                            style="margin-top: 3%;overflow: auto;max-height: 320px">
                    </el-tree>
                </el-tab-pane>
                <el-tab-pane label="个人数据集" name="second" style="height: 100%;max-height: 380px">
                    <el-table :data="personalData" style="margin-top: 1.5%;max-height: 320px;height: 280px"
                              @current-change="handleSelectionChange"
                              highlight-current-row>
                        <el-table-column
                                prop="datasetname"
                                label="数据名称"
                                width="300">
                        </el-table-column>
                        <el-table-column
                                prop="createtime"
                                label="创建时间"
                                width="200"
                                align="center">
                        </el-table-column>
                        <el-table-column
                                prop="successnumb"
                                label="记录数"
                                width="100"
                                align="center">
                        </el-table-column>
                    </el-table>
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="pageNo"
                            :page-sizes="[5, 10, 15, 20]"
                            :page-size="pageSize"
                            background
                            layout="prev, pager, next"
                            :total=this.totalCount
                            style="text-align: center;margin-top: 10px">
                    </el-pagination>
                </el-tab-pane>
            </el-tabs>
        </div>
        <el-button @click="confirm" type="primary" style="margin-top: 4%">确定</el-button>
        <el-button @click="fileVisible=false" type="primary">取消</el-button>
    </el-dialog>
</template>

<script>
    export default {
        name: "fileChooseDialog",
        data() {
            return {
                fileVisible: false,
                activeName: 'first',
                props: {
                    label: 'label',
                    children: 'children',
                    isLeaf: 'leaf'
                },
                data: [],
                personalData: [],
                pageSize: 5,
                pageNo: 1,
                totalCount: 0,
                fileParam: ''
            }
        },
        watch: {
            pageNo() {
                this.getPersonalData();
            },
            pageSize() {
                this.getPersonalData();
            }
        },
        methods: {
            handleSizeChange(val) {
                this.pageSize = val;
            },
            handleCurrentChange(val) {
                this.pageNo = val;
            },
            handleClick(val) {
                if (val === 'first') {

                }
            },
            handleNode(data, node) {
                this.fileParam = '';
                let currentNode = node;
                if (data.type !== "Directory") {
                    if (node.level === 1) {
                        this.fileParam = "/" + data.label;
                    } else {
                        for (let i = 0; i < currentNode.level; i++) {
                            this.fileParam = "/" + node.label + this.fileParam;
                            node = node.parent;
                        }
                    }
                }
                console.log(this.fileParam)
            },
            handleSelectionChange(val) {
                this.fileParam = val;
            },
            getPersonalData() {
                this.personalData=[];
                let that = this;
                this.$axios.get(that.$URL.datasetMoniterListFinishUrl + that.pageSize + "&pageNo=" + this.pageNo)
                    .then(res => {
                        for (let row of res.body.result) {
                            if (row.status === "Finish") {
                                that.personalData.push(row);
                            }
                        }
                        that.totalCount = res.body.totalCount;
                    }).catch(function (error) {
                    console.log(error);
                    that.$message.error('数据获取失败！')
                })
            },
            loadNode(node, resolve) {
                let data = [];
                let tempNode = node;
                let path = "";
                if (node.level === 0) {
                    path = "/";
                } else {
                    if (node.data.type === "File") {
                        return resolve([]);
                    } else {
                        for (let i = 0; i < node.level; i++) {
                            path = "/" + tempNode.label + path;
                            tempNode = tempNode.parent;
                        }
                    }
                }
                let url = this.$URL.fileGetUrl + path;
                this.$axios.get(url)
                    .then(function (res) {
                        if (res.code === 200) {
                            for (let item of res.body) {
                                let file = {};
                                file.label = item.Name;
                                file.type = item.Type;
                                file.children = [""];
                                data.push(file);
                            }
                            return resolve(data);
                        } else {
                            return resolve([]);
                        }
                    });
            },
            confirm() {
                if (this.fileParam === '') {
                    this.$message({
                        type: 'warning',
                        message: '请选择正确的文件！'
                    })
                } else {
                    this.$emit('getFile', this.fileParam);
                    this.fileParam = "";
                    this.fileVisible = false;
                }
                this.$refs.tree.setCheckedKeys([]);
            }
        },
        created() {
            let that = this;
            this.$Bus.$on('fileVisible', res => {
                that.fileVisible = res;
            });
            this.getPersonalData();
        }
    }
</script>

<style>
    .el-tree > .el-tree-node {
        display: inline-block;
        min-width: 100%;
    }
</style>